using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
using System;

public class SuitMO {
    static List<Suit> _suitDataList = null;//所有装备数据

    private static void InitData()
    {
        if (_suitDataList != null) return;
        //读取装备数据
        _suitDataList = Utils.LoadByJson<List<Suit>>(Application.dataPath, "suitList.json");
        if (_suitDataList == null) _suitDataList = new List<Suit>();
        //重新创建单个大项属性，保存为json后转回来，就变成父类的对象
        for(int i = 0;i < _suitDataList.Count;i++){
            for(int j = 0;j < _suitDataList[i].numProperty.Count;j++){
                // var name1 = _suitDataList[i].numProperty[j].className;
                // var name2 = _suitDataList[i].numProperty[j].GetType().FullName;
                // if(name1 != name2){
                    // _suitDataList[i].numProperty[j].className = "SuitNum";
                    var propertyList = _suitDataList[i].numProperty[j].propertyList;
                    var assembly = Utils.GetCurAssembly();
                    _suitDataList[i].numProperty[j] = (SkillBase)assembly.CreateInstance(_suitDataList[i].numProperty[j].className); 
                    _suitDataList[i].numProperty[j].InitData(propertyList);
                // }
            }
        }
    }
    /// <summary>
    /// 获得所有套装数据
    /// </summary>
    /// <returns></returns>
    public static List<Suit> GetSuitList(){
        InitData();
        return _suitDataList;
    }
    /// <summary>
    /// 保存装备列表
    /// </summary>
    public static void SaveSuitDataList(){
        if(_suitDataList == null) return;
        Utils.SaveByJson<List<Suit>>(Application.dataPath, "suitList.json",_suitDataList);
    }
    /// <summary>
    /// 保存装备
    /// </summary>
    /// <param name="suitData"></param>
    /// <param name="index"></param>
    /// <returns></returns>
    public static bool SaveSuit(Suit suitData,int index)
    {
        //新增装备的保存
        if(index == _suitDataList.Count) {
            _suitDataList.Add(suitData);
        }
        else{
            if (index < 0 || index >= _suitDataList.Count) return false;
            _suitDataList[index] = suitData;
        }
        SaveSuitDataList();
        return true;
    }
    /// <summary>
    /// 删除装备
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public static bool DelSuit(int index){
        if (index < 0 || index >= _suitDataList.Count) return false;
        LTLocalization.DelKey(_suitDataList[index].name);
        _suitDataList.RemoveAt(index);
        //ResetSuitId();
        SaveSuitDataList();
        LTLocalization.SaveDataToCSV();
        return true;
    }
    /// <summary>
    /// 重置装备id
    /// </summary>
    public static void ResetSuitId(){
        if(_suitDataList == null) return;
        for(int i = 0;i < _suitDataList.Count;i++){
            _suitDataList[i].id = "Suit_"+(i + 1);
            ResetSuitName(_suitDataList[i]);
        }
    }
    /// <summary>
    /// 重置装备名字键值
    /// </summary>
    /// <param name="suit"></param>
    public static void ResetSuitName(Suit suit)
    {
        var newName = suit.id + "_name";
        if(newName != suit.name)
        {
            var isContain = LTLocalization.IsContain(suit.name);
            if (isContain)
            {
                LTLocalization.ReplaceKey(newName,suit.name);
                LTLocalization.DelKey(suit.name);
            }
            suit.name = newName;
        }
    }
    public static int GetSuitIndex(Suit suit){
        InitData();
        return _suitDataList.IndexOf(suit);
    }
    /// <summary>
    /// 获取指定id的数据
    /// </summary>
    /// <param name="suitId"></param>
    /// <returns></returns>
    public static Suit GetSuitById(string suitId){
        InitData();
        foreach (var item in _suitDataList){
            if(item.id == suitId) return item;
        }
        return null;
    }
    public static List<Suit> GetSuitByWeapon(string weaponId){
        InitData();
        var suitList = new List<Suit>();
        foreach(var item in _suitDataList){
            if(item.weaponList.Contains(weaponId)) suitList.Add(item);
        }
        return suitList;
    }
    public static void RemoveAllPointWeapon(string weaponId){
        InitData();
        foreach(var item in _suitDataList){
            item.RemoveWeapon(weaponId);
        }
        SaveSuitDataList();
    }
    public static void RevemoSuitWeapon(string suitId,string weaponId){
        var suit = GetSuitById(suitId);
        if(suit != null){
            suit.RemoveWeapon(weaponId);
            SaveSuitDataList();
        }
    }
    public static void AddWeaponToSuit(string suitId,string weaponId){
        var suit = GetSuitById(suitId);
        if(suit != null){
            suit.AddWeapon(weaponId);
            SaveSuitDataList();
        }
    }
}
